package cn.school.sys.service.impl;


import cn.school.common.api.CommonResult;
import cn.school.sys.dao.SysAppVersionDao;
import cn.school.sys.entity.SysAppVersion;
import cn.school.sys.service.SysAppVersionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class SysAppVersionServiceImpl extends ServiceImpl<SysAppVersionDao, SysAppVersion> implements SysAppVersionService {

    @Autowired
    SysAppVersionDao appVersionDao;

    @Override
    public IPage<SysAppVersion> findAppVersionByPageList(SysAppVersion appVersion) {
        QueryWrapper<SysAppVersion> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(appVersion.getVersionName())){
            wrapper.eq("version_name",appVersion.getVersionName());
        }
        return page(new Page<>(appVersion.getPageNum(),appVersion.getPageSize()),wrapper);
    }

    @Override
    public Integer createAppVersion(SysAppVersion appVersion) {
        appVersion.setCreateTime(new Date());
        return appVersionDao.insert(appVersion);
    }

    @Override
    public Integer deleteAppVersion(Long id) {
        return appVersionDao.deleteById(id);
    }

    @Override
    public CommonResult compareVersion(SysAppVersion appVersion) {
        QueryWrapper<SysAppVersion> wrapper = new QueryWrapper<>();
        wrapper.eq("app_type",appVersion.getAppType());
        wrapper.orderByDesc("version_no").last("limit 1");
        SysAppVersion sysAppVersion = appVersionDao.selectOne(wrapper);
        if(sysAppVersion.getVersionNo() > appVersion.getVersionNo()){
            return CommonResult.success(sysAppVersion);
        }else{
            return CommonResult.success();
        }
    }
}
